<!doctype html>



  


<html class="theme-next pisces use-motion">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />












  
  
  <link href="/vendors/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/vendors/font-awesome/css/font-awesome.min.css?v=4.4.0" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.0.1" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Hexo, NexT" />





  <link rel="alternate" href="/atom.xml" title="liujinlongxa的技术博客" type="application/atom+xml" />




  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.0.1" />






<meta name="description" content="第一章：核心动画概念CoreAnimation框架中类的分类

提供显示内容的图层类
动画和计时类
布局和约束类
事务类，在原子更新的时候组合图层类

图层类（Layer Classes）CALayer是所有层类的父类，几个常见的图层子类：CAScrollLayer,CATextLayer,CATiledLayer等
动画类和计时类CAAnimation是动画类的基类，他实现了CAMediaTim">
<meta property="og:type" content="article">
<meta property="og:title" content="CoreAnimation编程指南学习笔记(1)">
<meta property="og:url" content="http://yoursite.com/2015/12/06/CoreAnimation编程指南学习笔记(1)/index.html">
<meta property="og:site_name" content="liujinlongxa的技术博客">
<meta property="og:description" content="第一章：核心动画概念CoreAnimation框架中类的分类

提供显示内容的图层类
动画和计时类
布局和约束类
事务类，在原子更新的时候组合图层类

图层类（Layer Classes）CALayer是所有层类的父类，几个常见的图层子类：CAScrollLayer,CATextLayer,CATiledLayer等
动画类和计时类CAAnimation是动画类的基类，他实现了CAMediaTim">
<meta property="og:updated_time" content="2016-07-01T12:43:36.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="CoreAnimation编程指南学习笔记(1)">
<meta name="twitter:description" content="第一章：核心动画概念CoreAnimation框架中类的分类

提供显示内容的图层类
动画和计时类
布局和约束类
事务类，在原子更新的时候组合图层类

图层类（Layer Classes）CALayer是所有层类的父类，几个常见的图层子类：CAScrollLayer,CATextLayer,CATiledLayer等
动画类和计时类CAAnimation是动画类的基类，他实现了CAMediaTim">



<script type="text/javascript" id="hexo.configuration">
  var NexT = window.NexT || {};
  var CONFIG = {
    scheme: 'Pisces',
    sidebar: {"position":"left","display":"post"},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: 0,
      author: '博主'
    }
  };
</script>




  <link rel="canonical" href="http://yoursite.com/2015/12/06/CoreAnimation编程指南学习笔记(1)/"/>

  <title> CoreAnimation编程指南学习笔记(1) | liujinlongxa的技术博客 </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  










  
  
    
  

  <div class="container one-collumn sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-meta ">
  

  <div class="custom-logo-site-title">
    <a href="/"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">liujinlongxa的技术博客</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle"></p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      

      
    </ul>
  

  
</nav>

 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  
  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                CoreAnimation编程指南学习笔记(1)
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            <span class="post-meta-item-icon">
              <i class="fa fa-calendar-o"></i>
            </span>
            <span class="post-meta-item-text">发表于</span>
            <time itemprop="dateCreated" datetime="2015-12-06T00:00:00+08:00" content="2015-12-06">
              2015-12-06
            </time>
          </span>

          
            <span class="post-category" >
              &nbsp; | &nbsp;
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
              
                <span itemprop="about" itemscope itemtype="https://schema.org/Thing">
                  <a href="/categories/读书笔记/" itemprop="url" rel="index">
                    <span itemprop="name">读书笔记</span>
                  </a>
                </span>

                
                

              
            </span>
          

          
            
              <span class="post-comments-count">
                &nbsp; | &nbsp;
                <a href="/2015/12/06/CoreAnimation编程指南学习笔记(1)/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count ds-thread-count" data-thread-key="2015/12/06/CoreAnimation编程指南学习笔记(1)/" itemprop="commentsCount"></span>
                </a>
              </span>
            
          

          

          
          

          
        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="第一章：核心动画概念"><a href="#第一章：核心动画概念" class="headerlink" title="第一章：核心动画概念"></a>第一章：核心动画概念</h2><p>CoreAnimation框架中类的分类</p>
<ul>
<li>提供显示内容的图层类</li>
<li>动画和计时类</li>
<li>布局和约束类</li>
<li>事务类，在原子更新的时候组合图层类</li>
</ul>
<h3 id="图层类（Layer-Classes）"><a href="#图层类（Layer-Classes）" class="headerlink" title="图层类（Layer Classes）"></a>图层类（Layer Classes）</h3><p>CALayer是所有层类的父类，几个常见的图层子类：CAScrollLayer,CATextLayer,CATiledLayer等</p>
<h3 id="动画类和计时类"><a href="#动画类和计时类" class="headerlink" title="动画类和计时类"></a>动画类和计时类</h3><p>CAAnimation是动画类的基类，他实现了CAMediaTiming协议，提供了动画的持续时间，速度，和重复计数等。他也实现了CAAction协议，该协议为图层触发一个动画动作提供了标准化响应。</p>
<p>动画类同时定义了一个使用贝塞尔曲线来描述动画改变的时间函数。</p>
<ul>
<li>CATransition：提供了一个图层改变的过渡效果，他能影响图层的整个内容。</li>
<li>CAAnimationGroup:组动画</li>
<li>CAPropertyAnimation:属性动画</li>
<li>CABasicAnimation:基础动画</li>
<li>CAKeyframeAnimation:关键帧动画</li>
</ul>
<h3 id="布局管理类"><a href="#布局管理类" class="headerlink" title="布局管理类"></a>布局管理类</h3><p>CAConstraint类是一个布局管理类，他可以指定子图层类限制于你指定的约束集合。</p>
<h3 id="事务管理类"><a href="#事务管理类" class="headerlink" title="事务管理类"></a>事务管理类</h3><p>CATransaction是核心动画里面负责协调多个动画原子更新显示操作。事务支持嵌套使用。核心动画支持两种事务：隐式事务和显式事务。</p>
<h2 id="第二章：渲染架构"><a href="#第二章：渲染架构" class="headerlink" title="第二章：渲染架构"></a>第二章：渲染架构</h2><p>每个可见的图层树由两个相应的树组成，一个是呈现树，一个是渲染树。修改图层的一个属性，图层树里相应的值会被马上更新，但是呈现树里面的值在将要显示给用户的时候才会被更新为新值。</p>
<h2 id="第三章：几何变换"><a href="#第三章：几何变换" class="headerlink" title="第三章：几何变换"></a>第三章：几何变换</h2><p>anchorPoint:锚点，他指定了bounds相对于position的值，同时也作为变换时候的支点。在Autolayout设置约束时，锚点的值也会影响视图显示的位置。</p>
<p>几何变换：CATransform3D，可以使用变换函数进行变换，例如CATransform3DMakeTranslation，CATransform3DTranslate等，也可以通过键值路径修改变换。</p>
<h2 id="第四章：图层树结构"><a href="#第四章：图层树结构" class="headerlink" title="第四章：图层树结构"></a>第四章：图层树结构</h2><p>图层树与视图的结构很类似：</p>
<ul>
<li>每个图层定义了一个基于其父图层的坐标系的坐标系，当一个图层变换的时候，他的子图层同样变换</li>
<li>一个动态的图层树，可以在程序运行的时候重新设置，图层可以添加，也可以删除</li>
</ul>
<p>ios中，每个UIView的实例会自动创建一个CALayer类的实体，视图为图层提供了底层的事件处理，图层为视图提供了显示的内容。</p>
<h2 id="第五章：图层的内容"><a href="#第五章：图层的内容" class="headerlink" title="第五章：图层的内容"></a>第五章：图层的内容</h2><p>使用CALayer无需创建继承子类，直接设置CALayer的contents属性即可，contents属性是一个CGImageRef类型，这样子创建比使用UIImageView效率要高</p>
<figure class="highlight objectivec"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">self</span>.myLayer = [[<span class="built_in">CALayer</span> alloc] init];</div><div class="line"><span class="keyword">self</span>.myLayer.bounds = <span class="built_in">CGRectMake</span>(<span class="number">0</span>, <span class="number">0</span>, <span class="number">100</span>, <span class="number">100</span>);</div><div class="line"><span class="keyword">self</span>.myLayer.position = <span class="built_in">CGPointMake</span>(<span class="number">100</span>, <span class="number">100</span>);</div><div class="line"><span class="keyword">self</span>.myLayer.backgroundColor = [<span class="built_in">UIColor</span> redColor].CGColor;</div><div class="line"><span class="built_in">UIImage</span> *image = [<span class="built_in">UIImage</span> imageNamed:<span class="string">@"1.JPG"</span>];</div><div class="line"><span class="keyword">self</span>.myLayer.contents = (__bridge <span class="keyword">id</span> _Nullable)(image.CGImage);</div><div class="line">[<span class="keyword">self</span>.view.layer addSublayer:<span class="keyword">self</span>.myLayer];</div></pre></td></tr></table></figure>
<p>也可以通过代理方法，提供CALayer的内容，有两个代理方法：<code>- (void)displayLayer:(CALayer *)theLayer</code>与<code>- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx</code>, 要通知系统重绘图层，只需要调用CALayer的<code>setNeedsDisplay</code>或者<code>setNeedsDisplayInRect:</code>方法或者把图层的needsDisplayOnBoundsChange属性值设置为YES即可。</p>
<figure class="highlight objectivec"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div></pre></td><td class="code"><pre><div class="line">- (<span class="keyword">void</span>)drawLayer:(<span class="built_in">CALayer</span> *)layer inContext:(<span class="built_in">CGContextRef</span>)ctx &#123;</div><div class="line">    <span class="built_in">CGMutablePathRef</span> thePath = <span class="built_in">CGPathCreateMutable</span>();</div><div class="line">    <span class="built_in">CGPathMoveToPoint</span>(thePath,<span class="literal">NULL</span>,<span class="number">15.0</span>f,<span class="number">15.</span>f);</div><div class="line">    <span class="built_in">CGPathAddCurveToPoint</span>(thePath,</div><div class="line">                          <span class="literal">NULL</span>,</div><div class="line">                          <span class="number">15.</span>f,<span class="number">250.0</span>f,</div><div class="line">                          <span class="number">295.0</span>f,<span class="number">250.0</span>f,</div><div class="line">                          <span class="number">295.0</span>f,<span class="number">15.0</span>f);</div><div class="line">    <span class="built_in">CGContextBeginPath</span>(ctx);</div><div class="line">    <span class="built_in">CGContextAddPath</span>(ctx, thePath );</div><div class="line">    <span class="built_in">CGContextSetLineWidth</span>(ctx, <span class="number">5</span>);</div><div class="line">    <span class="built_in">CGContextStrokePath</span>(ctx);</div><div class="line">    <span class="comment">// release the path</span></div><div class="line">    <span class="built_in">CFRelease</span>(thePath);</div><div class="line">&#125;</div><div class="line"></div><div class="line">- (<span class="keyword">void</span>)displayLayer:(<span class="built_in">CALayer</span> *)layer &#123;</div><div class="line">    <span class="built_in">UIImage</span> *image = [<span class="built_in">UIImage</span> imageNamed:<span class="string">@"1.JPG"</span>];</div><div class="line">    layer.contents = (__bridge <span class="keyword">id</span> _Nullable)(image.CGImage);</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>除了设置代理方法，也可以通过继承CALayer，重写<code>display</code>方法或<code>- (void)drawInContext:(CGContextRef)theContext</code>方法来自定义图层</p>
<p>CALayer的contentsGravity属性相当于UIView的contentMode属性。</p>

      
    </div>

    <div>
      
        
      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2015/11/07/ContentHugging与ContentCompressionResistance/" rel="next" title="Content Hugging与Content Compression Resistance">
                <i class="fa fa-chevron-left"></i> Content Hugging与Content Compression Resistance
              </a>
            
          </div>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2015/12/13/CoreAnimation编程指南学习笔记(2)/" rel="prev" title="CoreAnimation编程指南学习笔记(2)">
                CoreAnimation编程指南学习笔记(2) <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
      <div class="ds-thread" data-thread-key="2015/12/06/CoreAnimation编程指南学习笔记(1)/"
           data-title="CoreAnimation编程指南学习笔记(1)" data-url="http://yoursite.com/2015/12/06/CoreAnimation编程指南学习笔记(1)/">
      </div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel ">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/smallhead.png"
               alt="liujinlongxa" />
          <p class="site-author-name" itemprop="name">liujinlongxa</p>
          <p class="site-description motion-element" itemprop="description">生命永不息，Coding永不止</p>
        </div>
        <nav class="site-state motion-element">
          <div class="site-state-item site-state-posts">
            <a href="/archives">
              <span class="site-state-item-count">31</span>
              <span class="site-state-item-name">日志</span>
            </a>
          </div>

          
            <div class="site-state-item site-state-categories">
              <a href="/categories">
                <span class="site-state-item-count">3</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            <div class="site-state-item site-state-tags">
              <a href="/tags">
                <span class="site-state-item-count">3</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        
          <div class="feed-link motion-element">
            <a href="/atom.xml" rel="alternate">
              <i class="fa fa-rss"></i>
              RSS
            </a>
          </div>
        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/liujinlongxa" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="https://twitter.com/JaylonLiu" target="_blank" title="Twitter">
                  
                    <i class="fa fa-fw fa-twitter"></i>
                  
                  Twitter
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="http://weibo.com/u/1523828165/home" target="_blank" title="微博">
                  
                    <i class="fa fa-fw fa-globe"></i>
                  
                  微博
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="https://www.douban.com/people/2470188/" target="_blank" title="豆瓣">
                  
                    <i class="fa fa-fw fa-globe"></i>
                  
                  豆瓣
                </a>
              </span>
            
          
        </div>

        
        

        
        

      </section>

      
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">
            
              
            
            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#第一章：核心动画概念"><span class="nav-number">1.</span> <span class="nav-text">第一章：核心动画概念</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#图层类（Layer-Classes）"><span class="nav-number">1.1.</span> <span class="nav-text">图层类（Layer Classes）</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#动画类和计时类"><span class="nav-number">1.2.</span> <span class="nav-text">动画类和计时类</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#布局管理类"><span class="nav-number">1.3.</span> <span class="nav-text">布局管理类</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#事务管理类"><span class="nav-number">1.4.</span> <span class="nav-text">事务管理类</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#第二章：渲染架构"><span class="nav-number">2.</span> <span class="nav-text">第二章：渲染架构</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#第三章：几何变换"><span class="nav-number">3.</span> <span class="nav-text">第三章：几何变换</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#第四章：图层树结构"><span class="nav-number">4.</span> <span class="nav-text">第四章：图层树结构</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#第五章：图层的内容"><span class="nav-number">5.</span> <span class="nav-text">第五章：图层的内容</span></a></li></ol></div>
            
          </div>
        </section>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2016</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">liujinlongxa</span>
</div>

<div class="powered-by">
  由 <a class="theme-link" href="http://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Pisces
  </a>
</div>

        

        
      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  



  
  <script type="text/javascript" src="/vendors/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/vendors/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/vendors/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/vendors/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/vendors/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/vendors/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.0.1"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.0.1"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.0.1"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.0.1"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.0.1"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.0.1"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.0.1"></script>



  

  
    
  

  <script type="text/javascript">
    var duoshuoQuery = {short_name:"liujinlongxa"};
    (function() {
      var ds = document.createElement('script');
      ds.type = 'text/javascript';ds.async = true;
      ds.id = 'duoshuo-script';
      ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
      ds.charset = 'UTF-8';
      (document.getElementsByTagName('head')[0]
      || document.getElementsByTagName('body')[0]).appendChild(ds);
    })();
  </script>

  
    
  






  
  
  

  

  

</body>
</html>
